<?php
/**
 * Application Common Component
 * @copyright  duonght6255@setacinq.com.vn
 *
 */
App::uses('Component', 'Controller');

class CommonComponent extends Component{

	/**
	 *
	 * @param unknown $str
	 * @return boolean|mixed
	 */
	public static function ConvertToUnicode($str){
		$vietnamese = array(
				"à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
				"ằ","ắ","ặ","ẳ","ẵ",
				"è","é","ẹ","ẻ","ẽ","ê","ề" ,"ế","ệ","ể","ễ",
				"ì","í","ị","ỉ","ĩ",
				"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
				,"ờ","ớ","ợ","ở","ỡ",
				"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
				"ỳ","ý","ỵ","ỷ","ỹ",
				"đ",
				"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
				,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
				"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
				"Ì","Í","Ị","Ỉ","Ĩ",
				"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
				,"Ờ","Ớ","Ợ","Ở","Ỡ",
				"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
				"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
				"Đ","ê","ù","à"
		);

		$unicode = array(
				"a","a","a","a","a","a","a","a","a","a","a"
				,"a","a","a","a","a","a",
				"e","e","e","e","e","e","e","e","e","e","e",
				"i","i","i","i","i",
				"o","o","o","o","o","o","o","o","o","o","o","o"
				,"o","o","o","o","o",
				"u","u","u","u","u","u","u","u","u","u","u",
				"y","y","y","y","y",
				"d",
				"A","A","A","A","A","A","A","A","A","A","A","A"
				,"A","A","A","A","A",
				"E","E","E","E","E","E","E","E","E","E","E",
				"I","I","I","I","I",
				"O","O","O","O","O","O","O","O","O","O","O","O"
				,"O","O","O","O","O",
				"U","U","U","U","U","U","U","U","U","U","U",
				"Y","Y","Y","Y","Y",
				"D","e","u","a"
		);

		return str_replace($vietnamese, $unicode, $str);
	}

	/**
	 * Seo title
	 *
	 * @param string $string
	 * @return string
	 */
	public static function rewriteTitle($string, $keyReplace = '/') {
		$string = self::ConvertToUnicode($string);

		$string = trim(preg_replace("/[^A-Za-z0-9]/i"," ",$string)); // khong dau
		$string = str_replace(" ", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace("--", "-", $string);
		$string = str_replace($keyReplace, "-", $string);

		return strtolower(trim($string, '-'));
	}

	/**
	 * Format category follow parent and child
	 * @param array $categories
	 * @return array
	 */
	public static function formatCategory($categories = array()){
		foreach ($categories as &$category) {
			if($category['cat_level'] >= 1){
				$category['cat_name'] = str_repeat('---',$category['cat_level']).$category['cat_name'];
			}
		}
		return $categories;
	}

	/**
	 * Xử lý cùng độ dài ở dạng text
	 * ==> 1 ==> 001; ==> 2 ==> 002 ....
	 * @param unknown $length
	 * @param unknown $number
	 * @return string
	 */
	public static function sameLengthTextNumber($length, $number){
		if($length > strlen($number)){
			$add_length = $length - strlen($number);
			$number = str_repeat('0', $add_length).$number;
		}

		return $number;
	}

	/**
	 *
	 * @param unknown $items
	 * @param unknown $field
	 * @param string $key
	 * @return Ambigous <multitype:, unknown>
	 */
	public static function classifyArray($items, $field, $key = false){
		$result = array();
		foreach ($items as $item) {
			if(!empty($key)){
				$result[$item[$field]][$item[$key]] = $item;
			}else {
				$result[$item[$field]][] = $item;
			}
		}

		return $result;
	}
}
